package com.happyass.show.service.impl;

import com.happyass.show.dao.ActivityDao;
import com.happyass.show.dao.PlanDao;
import com.happyass.show.entity.Activity;
import com.happyass.show.entity.Plan;
import com.happyass.show.entity.PlanActRel;
import com.happyass.show.service.PlanService;
import net.minidev.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * TODO
 *
 * @author admin
 * @date 2021/1/4 18:56
 *
 * <P></P>
 **/

@Service
public class PlanServiceImpl implements PlanService {

    @Autowired
    private PlanDao planDao;
    @Autowired
    private ActivityDao activityDao;


    @Override
    public List<Plan> findAll() {
        return planDao.findAll();
    }


    //前台当前计划演出时间安排查看
    @Override
    public HashMap<String, Object> showTimeList() {
        String planItem = planDao.findByPlanStatus(1).getPlanItem();
        Plan plan = planDao.findByPlanItem(planItem);
        List<PlanActRel> pars = plan.getPlanActRels();

        Set<String> addrDay = new HashSet<>();
        Set<String> addrNight = new HashSet<>();

        for (PlanActRel par : pars) {
            if (par.getActivity().getDayTime()==1){
                String addrName = par.getActivity().getAddr().getAddrName();
                addrDay.add(addrName);
            }else if (par.getActivity().getDayTime()==2){
                String addrName = par.getActivity().getAddr().getAddrName();
                addrNight.add(addrName);
            }
        }


        HashMap<String, Object> dayMap = new HashMap<>();
        HashMap<String, Object> nightMap = new HashMap<>();

        for (String addrD : addrDay) {
            JSONArray jsonArray = new JSONArray();
            for (PlanActRel par : pars) {
                Activity activity = par.getActivity();
                //白天场
                if (activity.getDayTime() == 1) {
                    if (activity.getAddr().getAddrName().equals(addrD)) {
                        HashMap<String, Object> hm = new HashMap<>();
                        hm.put("activityName", activity.getActivityName());
                        hm.put("activityArea", activity.getActivityArea());

                        if(par.getShowWorkdayTime().equals("null")){
                            hm.put("showWorkdayTime","");
                        }else {
                            hm.put("showWorkdayTime", "  工作日演出时间: "+par.getShowWorkdayTime());
                        }

                        if(par.getShowWeekdayTime().equals("null")){
                            hm.put("showWeekdayTime","");
                        }else {
                            hm.put("showWeekdayTime", "  周末演出时间: "+par.getShowWeekdayTime());
                        }

                        if(par.getShowStopWeekday().equals("null")){
                            hm.put("showStopWeekday","");
                        }else {
                            hm.put("showStopWeekday", "(" + par.getShowStopWeekday() + "停演)");
                        }

                        jsonArray.add(hm);
                    }
                }
            dayMap.put(addrD, jsonArray);
            }
        }

            for (String addrN : addrNight) {
                JSONArray jsonArray = new JSONArray();
                for (PlanActRel par : pars) {
                    Activity activity = par.getActivity();
                    //夜间场
                    if (activity.getDayTime() == 2) {
                        if (activity.getAddr().getAddrName().equals(addrN)) {
                            HashMap<String, Object> hm = new HashMap<>();
                            hm.put("activityName", activity.getActivityName());
                            hm.put("activityArea", activity.getActivityArea());

                            hm.put("showWorkdayTime", par.getShowWorkdayTime());
                            hm.put("showWeekdayTime", par.getShowWeekdayTime());
                            if(par.getShowWorkdayTime().equals("null")){
                                hm.put("showWorkdayTime","");
                            }else {
                                hm.put("showWorkdayTime", "  工作日演出时间: "+par.getShowWorkdayTime());
                            }

                            if(par.getShowWeekdayTime().equals("null")){
                                hm.put("showWeekdayTime","");
                            }else {
                                hm.put("showWeekdayTime", "  周末演出时间: "+par.getShowWeekdayTime());
                            }

                            if(par.getShowStopWeekday().equals("null")){
                                hm.put("showStopWeekday","");
                            }else {
                                hm.put("showStopWeekday", "(" + par.getShowStopWeekday() + "停演)");
                            }
                            jsonArray.add(hm);
                        }
                    }
                    nightMap.put(addrN, jsonArray);
                }
            }

            HashMap<String, Object> all = new HashMap<>();
            all.put("dayMap", dayMap);
            all.put("nightMap", nightMap);

        return all;

    }

    //后台演出计划查看
    @Override
    public HashMap<String, Object> planList(Plan plan) {
        HashMap<String, Object> map = new HashMap<>();
        Plan planUsing = planDao.findByPlanStatus(1);
        map.put("planUsing",planUsing);

        //设置分页的参数
        Pageable pageable = PageRequest.of(plan.getPage()-1,plan.getRow());
        Page<Plan> planPage = planDao.findByPlanStatusAndPlanDel(0,1,pageable);

        //当前页
        map.put("planListStop",planPage.getContent());
        //数据总条数
        map.put("total",planPage.getTotalElements());
        //页数
        map.put("totalPage",planPage.getTotalPages());
        System.out.println("页数"+planPage.getTotalPages());
        //上一页
        if(planPage.getNumber() <=0 ){
            map.put("prePage",planPage.getNumber()+1);
        }else{
            map.put("prePage",planPage.getNumber());
        }
        //下一页
        if((planPage.getNumber()+2) >= planPage.getTotalPages()){
            map.put("nextPage",planPage.getTotalPages());
        }else{
            map.put("nextPage",planPage.getNumber()+2);
        }
        //当前页
        map.put("curPage",planPage.getNumber()+1);
        return map;

    }

    //后台演出计划状态修改
    @Override
    public HashMap<String, Object> planStatus(String planItem, Integer planStatus) {
        HashMap<String,Object> map = new HashMap<>();
        Plan planNew = planDao.findByPlanItem(planItem);
        if (planNew.getPlanStatus()!=planStatus){
            Plan planOld = planDao.findByPlanStatus(1);
            planOld.setPlanStatus(0);
            planNew.setPlanStatus(planStatus);
            planDao.save(planNew);
            planDao.save(planOld);
            map.put("info","success");
        }else {
            map.put("info","fail");
        }
        return map;
    }

    //后台演出计划新增
    @Override
    public HashMap<String, Object> planAdd(String activityId, String showWorkdayTime, String showWeekdayTime, String showStopWeekday, String planItem) {
        HashMap<String,Object> map = new HashMap<>();
        Plan p = new Plan();
        p.setPlanItem(planItem);
        planDao.save(p);
        Plan plan = planDao.findByPlanItem(planItem);
        plan.setPlanStatus(0);
        plan.setPlanDel(1);
        String[] activityIdArr = activityId.split(",");
        String[] showWorkdayTimeArr = showWorkdayTime.split(",");
        String[] showWeekdayTimeArr = showWeekdayTime.split(",");
        String[] showStopWeekdayArr = showStopWeekday.split(",");
        List<PlanActRel> parList = new ArrayList<>();
        for (int i = 0; i < activityIdArr.length; i++) {
            PlanActRel par = new PlanActRel();
            if (showWorkdayTimeArr[i]==null){
                showWorkdayTimeArr[i]="null";
            }else if (showWeekdayTimeArr[i]==null){
                showWeekdayTimeArr[i]="null";
            }else if (showStopWeekdayArr[i]==null){
                showStopWeekdayArr[i]="null";
            }
            par.setActivity(activityDao.findByActivityId(Integer.parseInt(activityIdArr[i])));
            par.setPlan(plan);
            par.setShowWorkdayTime(showWorkdayTimeArr[i]);
            par.setShowWeekdayTime(showWeekdayTimeArr[i]);
            par.setShowStopWeekday(showStopWeekdayArr[i]);
            parList.add(par);
        }
        if (!parList.isEmpty()){
            plan.setPlanActRels(parList);
            planDao.save(plan);
            map.put("info","success");
        }else {
            map.put("info","fail");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> planDel(String planItem) {
        HashMap<String,Object> map = new HashMap<>();
        Plan plan = planDao.findByPlanItem(planItem);
        //伪删除 1为未删除 0为已删除
        if (plan.getPlanDel()==1){
            plan.setPlanDel(0);
            planDao.save(plan);
            map.put("info","success");
        }else {
            map.put("info","fail");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> plansDel(String planItems) {
        HashMap<String,Object> map = new HashMap<>();
        String[] split = planItems.split(",");
        List<String> planItemList = new ArrayList<>();
        for (String s : split) {
            planItemList.add(s);
        }
        String failPlanItem = "";
        int failTime=0;
        for (String planItem : planItemList) {
            if (planDao.findByPlanItem(planItem)!=null){
                Plan plan = planDao.findByPlanItem(planItem);
                //伪删除 1为未删除 0为已删除
                if (plan.getPlanDel()==1){
                    plan.setPlanDel(0);
                    planDao.save(plan);
                }else {
                    failPlanItem += planItem+" ";
                    failTime += 1;
                }
            }else {
                map.put("info","编号为 "+planItem+" 的计划不存在！");
                return map;
            }
        }
        if (failTime>0){
            map.put("info","编号为 "+failPlanItem+" 的计划删除失败，请重试");
        }else {
            map.put("info","success");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> planSearch(String searchKey, String searchValue, Plan plan) {
        HashMap<String, Object> map = new HashMap<>();
        Plan planUsing = planDao.findByPlanStatus(1);
        map.put("planUsing",planUsing);

        //设置分页的参数
        Pageable pageable = PageRequest.of(plan.getPage()-1,plan.getRow());
        Page<Plan> planPage = null;
        if (searchKey.equals("planItem")) {
            planPage = planDao.findByPlanItemContainingAndPlanDel(searchValue,1,pageable);
            //当前页
            map.put("planList",planPage.getContent());
        }else if (searchKey.equals("planStatus")){
            if (searchValue.equals("启用")){
                planPage = planDao.findByPlanStatus(1, pageable);
                //当前页
                map.put("planList",planPage.getContent());
            }else if (searchValue.equals("停用")){
                planPage = planDao.findByPlanStatus(0, pageable);
                //当前页
                map.put("planList",planPage.getContent());
            }else {
                planPage = planDao.findByPlanStatus(9, pageable);
                //当前页
                map.put("planList",planPage.getContent());
            }
        }else {
            Plan planUsed = planDao.findByPlanStatus(1);
            map.put("planUsing",planUsed);
            planPage = planDao.findByPlanStatusAndPlanDel(0,1,pageable);
            map.put("back",1);
            //当前页
            map.put("planListStop",planPage.getContent());
        }

        //数据总条数
        map.put("total",planPage.getTotalElements());
        //页数
        map.put("totalPage",planPage.getTotalPages());
        System.out.println("页数"+planPage.getTotalPages());
        //上一页
        if(planPage.getNumber() <=0 ){
            map.put("prePage",planPage.getNumber()+1);
        }else{
            map.put("prePage",planPage.getNumber());
        }
        //下一页
        if((planPage.getNumber()+2) >= planPage.getTotalPages()){
            map.put("nextPage",planPage.getTotalPages());
        }else{
            map.put("nextPage",planPage.getNumber()+2);
        }
        //当前页
        map.put("curPage",planPage.getNumber()+1);
        map.put("searchValue",searchValue);
        map.put("searchKey",searchKey);
        return map;

    }

    @Override
    public HashMap<String,Object> update(String planItem) {
        HashMap<String,Object> map = new HashMap<>();
        Plan plan = planDao.findByPlanItem(planItem);
        List<PlanActRel> pars = plan.getPlanActRels();
        map.put("parList",pars);
        map.put("planItem",planItem);
        return map;
    }

    @Override
    public HashMap<String, Object> planUpdate(String activityId, String showWorkdayTime, String showWeekdayTime, String showStopWeekday, String planItem) {
        HashMap<String,Object> map = new HashMap<>();
        Plan plan = planDao.findByPlanItem(planItem);
        if (plan!=null){
            String[] activityIdArr = activityId.split(",");
            String[] showWorkdayTimeArr = showWorkdayTime.split(",");
            String[] showWeekdayTimeArr = showWeekdayTime.split(",");
            String[] showStopWeekdayArr = showStopWeekday.split(",");
            List<PlanActRel> parList = plan.getPlanActRels();
            for (PlanActRel par : parList) {
                for (int i = 0; i < activityIdArr.length; i++) {
                    if (par.getActivity().getActivityId()==Integer.parseInt(activityIdArr[i])){
                        if (showWorkdayTimeArr[i]==null){
                            showWorkdayTimeArr[i]="null";
                        }else if (showWeekdayTimeArr[i]==null){
                            showWeekdayTimeArr[i]="null";
                        }else if (showStopWeekdayArr[i]==null){
                            showStopWeekdayArr[i]="null";
                        }
                        par.setShowWorkdayTime(showWorkdayTimeArr[i]);
                        par.setShowWeekdayTime(showWeekdayTimeArr[i]);
                        par.setShowStopWeekday(showStopWeekdayArr[i]);
                    }
                }
            }
            plan.setPlanActRels(parList);
            planDao.save(plan);
            map.put("info","success");
        } else {
            map.put("info","fail");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> planDetail(String planItem) {
        Plan plan = planDao.findByPlanItem(planItem);
        List<PlanActRel> pars = plan.getPlanActRels();

        Set<String> addrDay = new HashSet<>();
        Set<String> addrNight = new HashSet<>();

        for (PlanActRel par : pars) {
            if (par.getActivity().getDayTime()==1){
                String addrName = par.getActivity().getAddr().getAddrName();
                addrDay.add(addrName);
            }else if (par.getActivity().getDayTime()==2){
                String addrName = par.getActivity().getAddr().getAddrName();
                addrNight.add(addrName);
            }
        }


        HashMap<String, Object> dayMap = new HashMap<>();
        HashMap<String, Object> nightMap = new HashMap<>();

        for (String addrD : addrDay) {
            JSONArray jsonArray = new JSONArray();
            for (PlanActRel par : pars) {
                Activity activity = par.getActivity();
                //白天场
                if (activity.getDayTime() == 1) {
                    if (activity.getAddr().getAddrName().equals(addrD)) {
                        HashMap<String, Object> hm = new HashMap<>();
                        hm.put("activityName", activity.getActivityName());
                        hm.put("activityArea", activity.getActivityArea());
                        hm.put("activityAddr", activity.getActivityAddr());

                        if(par.getShowWorkdayTime().equals("null")){
                            hm.put("showWorkdayTime","/");
                        }else {
                            hm.put("showWorkdayTime", par.getShowWorkdayTime());
                        }

                        if(par.getShowWeekdayTime().equals("null")){
                            hm.put("showWeekdayTime","/");
                        }else {
                            hm.put("showWeekdayTime", par.getShowWeekdayTime());
                        }

                        if(par.getShowStopWeekday().equals("null")){
                            hm.put("showStopWeekday","/");
                        }else {
                            hm.put("showStopWeekday", par.getShowStopWeekday());
                        }

                        jsonArray.add(hm);
                    }
                }
                dayMap.put(addrD, jsonArray);
            }
        }

        for (String addrN : addrNight) {
            JSONArray jsonArray = new JSONArray();
            for (PlanActRel par : pars) {
                Activity activity = par.getActivity();
                //夜间场
                if (activity.getDayTime() == 2) {
                    if (activity.getAddr().getAddrName().equals(addrN)) {
                        HashMap<String, Object> hm = new HashMap<>();
                        hm.put("activityName", activity.getActivityName());
                        hm.put("activityArea", activity.getActivityArea());
                        hm.put("activityAddr", activity.getActivityAddr());

                        if(par.getShowWorkdayTime().equals("null")){
                            hm.put("showWorkdayTime","/");
                        }else {
                            hm.put("showWorkdayTime", par.getShowWorkdayTime());
                        }

                        if(par.getShowWeekdayTime().equals("null")){
                            hm.put("showWeekdayTime","/");
                        }else {
                            hm.put("showWeekdayTime", par.getShowWeekdayTime());
                        }

                        if(par.getShowStopWeekday().equals("null")){
                            hm.put("showStopWeekday","/");
                        }else {
                            hm.put("showStopWeekday", par.getShowStopWeekday());
                        }
                        jsonArray.add(hm);
                    }
                }
                nightMap.put(addrN, jsonArray);
            }
        }

        HashMap<String, Object> all = new HashMap<>();
        all.put("dayMap", dayMap);
        all.put("nightMap", nightMap);

        return all;
    }

    @Override
    public HashMap<String, Object> findRetrieve(Plan plan) {
        HashMap<String, Object> map = new HashMap<>();
        //设置分页的参数
        Pageable pageable = PageRequest.of(plan.getPage()-1,plan.getRow());
        Page<Plan> planPage = planDao.findByPlanStatusAndPlanDel(0,0,pageable);

        //当前页
        map.put("planListStop",planPage.getContent());
        //数据总条数
        map.put("total",planPage.getTotalElements());
        //页数
        map.put("totalPage",planPage.getTotalPages());
        System.out.println("页数"+planPage.getTotalPages());
        //上一页
        if(planPage.getNumber() <=0 ){
            map.put("prePage",planPage.getNumber()+1);
        }else{
            map.put("prePage",planPage.getNumber());
        }
        //下一页
        if((planPage.getNumber()+2) >= planPage.getTotalPages()){
            map.put("nextPage",planPage.getTotalPages());
        }else{
            map.put("nextPage",planPage.getNumber()+2);
        }
        //当前页
        map.put("curPage",planPage.getNumber()+1);
        return map;

    }

    @Override
    public HashMap<String, Object> planRes(String planItem) {
        HashMap<String,Object> map = new HashMap<>();
        Plan plan = planDao.findByPlanItem(planItem);
        //伪删除 1为未删除 0为已删除
        if (plan.getPlanDel()==0){
            plan.setPlanDel(1);
            planDao.save(plan);
            map.put("info","success");
        }else {
            map.put("info","fail");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> plansRes(String items) {
        HashMap<String,Object> map = new HashMap<>();
        String[] split = items.split(",");
        List<String> planItemList = new ArrayList<>();
        for (String s : split) {
            planItemList.add(s);
        }
        String failPlanItem = "";
        int failTimes=0;
        for (String planItem : planItemList) {
            if (planDao.findByPlanItem(planItem)!=null){
                Plan plan = planDao.findByPlanItem(planItem);
                //伪删除 1为未删除 0为已删除
                if (plan.getPlanDel()==0){
                    plan.setPlanDel(1);
                    planDao.save(plan);
                }else {
                    failPlanItem += planItem+" ";
                    failTimes += 1;
                }
            }else {
                map.put("info","编号为 "+planItem+" 的计划不存在！");
                return map;
            }
        }
        if (failTimes>0){
            map.put("info","编号为 "+failPlanItem+" 的计划恢复失败，请重试");
        }else {
            map.put("info","success");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> retrieveSearch(String searchKey, String searchValue, Plan plan) {
        HashMap<String, Object> map = new HashMap<>();

        //设置分页的参数
        Pageable pageable = PageRequest.of(plan.getPage()-1,plan.getRow());
        Page<Plan> planPage = null;
        if (searchKey.equals("planItem")) {
            planPage = planDao.findByPlanItemContainingAndPlanDel(searchValue,0,pageable);
            //当前页
            map.put("planListStop",planPage.getContent());
        }else {
            planPage = planDao.findByPlanStatusAndPlanDel(0,0,pageable);
            map.put("back",1);
            //当前页
            map.put("planListStop",planPage.getContent());
        }

        //数据总条数
        map.put("total",planPage.getTotalElements());
        //页数
        map.put("totalPage",planPage.getTotalPages());
        System.out.println("页数"+planPage.getTotalPages());
        //上一页
        if(planPage.getNumber() <=0 ){
            map.put("prePage",planPage.getNumber()+1);
        }else{
            map.put("prePage",planPage.getNumber());
        }
        //下一页
        if((planPage.getNumber()+2) >= planPage.getTotalPages()){
            map.put("nextPage",planPage.getTotalPages());
        }else{
            map.put("nextPage",planPage.getNumber()+2);
        }
        //当前页
        map.put("curPage",planPage.getNumber()+1);
        map.put("searchValue",searchValue);
        map.put("searchKey",searchKey);
        return map;

    }
}
